package com.oracle.mapper;

import com.oracle.dto.PbcgzGzdytbDTO;
import com.oracle.entity.PbcgzGzdytb;
import com.oracle.utils.YDPString;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.Map;

/**
 * 低电压治理明细填报表Mapper SQL提供类
 * 
 * @author System
 * @date 2025-09-05
 */
public class PbcgzGzdytbMapperProvider {

    private static final String TABLE_NAME = "PBCGZ_GZDYTB";

    /**
     * 插入SQL
     */
    public String insert(PbcgzGzdytb entity) {
        return new SQL() {{
            INSERT_INTO(TABLE_NAME);
            
            if (!YDPString.isEmpty(entity.getId())) {
                VALUES("ID", "#{id}");
            }
            if (!YDPString.isEmpty(entity.getReportWeek())) {
                VALUES("REPORT_WEEK", "#{reportWeek}");
            }
            if (!YDPString.isEmpty(entity.getContactPhone())) {
                VALUES("CONTACT_PHONE", "#{contactPhone}");
            }
            if (!YDPString.isEmpty(entity.getSubsidiaryCompany())) {
                VALUES("SUBSIDIARY_COMPANY", "#{subsidiaryCompany}");
            }
            if (!YDPString.isEmpty(entity.getCityBureau())) {
                VALUES("CITY_BUREAU", "#{cityBureau}");
            }
            if (!YDPString.isEmpty(entity.getCountyBureau())) {
                VALUES("COUNTY_BUREAU", "#{countyBureau}");
            }
            if (!YDPString.isEmpty(entity.getTransformerName())) {
                VALUES("TRANSFORMER_NAME", "#{transformerName}");
            }
            if (!YDPString.isEmpty(entity.getGisid())) {
                VALUES("GISID", "#{gisid}");
            }
            if (!YDPString.isEmpty(entity.getTransformerCapacity())) {
                VALUES("TRANSFORMER_CAPACITY", "#{transformerCapacity}");
            }
            if (!YDPString.isEmpty(entity.getTransformerNameShort())) {
                VALUES("TRANSFORMER_NAME_SHORT", "#{transformerNameShort}");
            }
            if (!YDPString.isEmpty(entity.getTransformerType())) {
                VALUES("TRANSFORMER_TYPE", "#{transformerType}");
            }
            if (!YDPString.isEmpty(entity.getLineName())) {
                VALUES("LINE_NAME", "#{lineName}");
            }
            if (!YDPString.isEmpty(entity.getRegionalFeatures())) {
                VALUES("REGIONAL_FEATURES", "#{regionalFeatures}");
            }
            if (!YDPString.isEmpty(entity.getLowVoltagePrediction())) {
                VALUES("LOW_VOLTAGE_PREDICTION", "#{lowVoltagePrediction}");
            }
            if (!YDPString.isEmpty(entity.getSpecialOperationLowVoltage())) {
                VALUES("SPECIAL_OPERATION_LOW_VOLTAGE", "#{specialOperationLowVoltage}");
            }
            if (!YDPString.isEmpty(entity.getLowVoltageCause())) {
                VALUES("LOW_VOLTAGE_CAUSE", "#{lowVoltageCause}");
            }
            if (!YDPString.isEmpty(entity.getPreventionMeasures())) {
                VALUES("PREVENTION_MEASURES", "#{preventionMeasures}");
            }
            if (!YDPString.isEmpty(entity.getRemark())) {
                VALUES("REMARK", "#{remark}");
            }
            if (!YDPString.isEmpty(entity.getStatus())) {
                VALUES("STATUS", "#{status}");
            }
            if (!YDPString.isEmpty(entity.getSubmitTime())) {
                VALUES("SUBMIT_TIME", "#{submitTime}");
            }
            if (!YDPString.isEmpty(entity.getUserId())) {
                VALUES("USER_ID", "#{userId}");
            }
            if (!YDPString.isEmpty(entity.getUserName())) {
                VALUES("USER_NAME", "#{userName}");
            }
            if (!YDPString.isEmpty(entity.getOrgId())) {
                VALUES("ORG_ID", "#{orgId}");
            }
            if (!YDPString.isEmpty(entity.getOrgName())) {
                VALUES("ORG_NAME", "#{orgName}");
            }
            
            // 默认值
            VALUES("CREATE_TIME", "TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS')");
            VALUES("UPDATE_TIME", "TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS')");
        }}.toString();
    }

    /**
     * 根据ID删除SQL
     */
    public String deleteById(Map<String, Object> params) {
        return new SQL() {{
            DELETE_FROM(TABLE_NAME);
            WHERE("ID = #{id}");
        }}.toString();
    }

    /**
     * 批量删除SQL
     */
    public String batchDelete(Map<String, Object> params) {
        @SuppressWarnings("unchecked")
        List<String> ids = (List<String>) params.get("ids");
        
        StringBuilder sql = new StringBuilder("DELETE FROM " + TABLE_NAME + " WHERE ID IN (");
        for (int i = 0; i < ids.size(); i++) {
            if (i > 0) {
                sql.append(", ");
            }
            sql.append("#{ids[").append(i).append("]}");
        }
        sql.append(")");
        
        return sql.toString();
    }

    /**
     * 更新SQL
     */
    public String update(PbcgzGzdytb entity) {
        return new SQL() {{
            UPDATE(TABLE_NAME);
            
            if (!YDPString.isEmpty(entity.getReportWeek())) {
                SET("REPORT_WEEK = #{reportWeek}");
            }
            if (!YDPString.isEmpty(entity.getContactPhone())) {
                SET("CONTACT_PHONE = #{contactPhone}");
            }
            if (!YDPString.isEmpty(entity.getSubsidiaryCompany())) {
                SET("SUBSIDIARY_COMPANY = #{subsidiaryCompany}");
            }
            if (!YDPString.isEmpty(entity.getCityBureau())) {
                SET("CITY_BUREAU = #{cityBureau}");
            }
            if (!YDPString.isEmpty(entity.getCountyBureau())) {
                SET("COUNTY_BUREAU = #{countyBureau}");
            }
            if (!YDPString.isEmpty(entity.getTransformerName())) {
                SET("TRANSFORMER_NAME = #{transformerName}");
            }
            if (!YDPString.isEmpty(entity.getGisid())) {
                SET("GISID = #{gisid}");
            }
            if (!YDPString.isEmpty(entity.getTransformerCapacity())) {
                SET("TRANSFORMER_CAPACITY = #{transformerCapacity}");
            }
            if (!YDPString.isEmpty(entity.getTransformerNameShort())) {
                SET("TRANSFORMER_NAME_SHORT = #{transformerNameShort}");
            }
            if (!YDPString.isEmpty(entity.getTransformerType())) {
                SET("TRANSFORMER_TYPE = #{transformerType}");
            }
            if (!YDPString.isEmpty(entity.getLineName())) {
                SET("LINE_NAME = #{lineName}");
            }
            if (!YDPString.isEmpty(entity.getRegionalFeatures())) {
                SET("REGIONAL_FEATURES = #{regionalFeatures}");
            }
            if (!YDPString.isEmpty(entity.getLowVoltagePrediction())) {
                SET("LOW_VOLTAGE_PREDICTION = #{lowVoltagePrediction}");
            }
            if (!YDPString.isEmpty(entity.getSpecialOperationLowVoltage())) {
                SET("SPECIAL_OPERATION_LOW_VOLTAGE = #{specialOperationLowVoltage}");
            }
            if (!YDPString.isEmpty(entity.getLowVoltageCause())) {
                SET("LOW_VOLTAGE_CAUSE = #{lowVoltageCause}");
            }
            if (!YDPString.isEmpty(entity.getPreventionMeasures())) {
                SET("PREVENTION_MEASURES = #{preventionMeasures}");
            }
            if (!YDPString.isEmpty(entity.getRemark())) {
                SET("REMARK = #{remark}");
            }
            if (!YDPString.isEmpty(entity.getStatus())) {
                SET("STATUS = #{status}");
            }
            if (!YDPString.isEmpty(entity.getSubmitTime())) {
                SET("SUBMIT_TIME = #{submitTime}");
            }
            if (!YDPString.isEmpty(entity.getUserId())) {
                SET("USER_ID = #{userId}");
            }
            if (!YDPString.isEmpty(entity.getUserName())) {
                SET("USER_NAME = #{userName}");
            }
            if (!YDPString.isEmpty(entity.getOrgId())) {
                SET("ORG_ID = #{orgId}");
            }
            if (!YDPString.isEmpty(entity.getOrgName())) {
                SET("ORG_NAME = #{orgName}");
            }
            
            SET("UPDATE_TIME = TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS')");
            WHERE("ID = #{id}");
        }}.toString();
    }

    /**
     * 批量更新状态SQL
     */
    public String batchUpdateStatus(Map<String, Object> params) {
        @SuppressWarnings("unchecked")
        List<String> ids = (List<String>) params.get("ids");
        
        StringBuilder sql = new StringBuilder();
        sql.append("UPDATE ").append(TABLE_NAME).append(" SET STATUS = #{status}");
        
        if (params.get("submitTime") != null) {
            sql.append(", SUBMIT_TIME = #{submitTime}");
        }
        
        sql.append(", UPDATE_TIME = TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS')");
        sql.append(" WHERE ID IN (");
        
        for (int i = 0; i < ids.size(); i++) {
            if (i > 0) {
                sql.append(", ");
            }
            sql.append("#{ids[").append(i).append("]}");
        }
        sql.append(")");
        
        return sql.toString();
    }

    /**
     * 根据ID查询SQL
     */
    public String selectById(Map<String, Object> params) {
        return new SQL() {{
            SELECT("*");
            FROM(TABLE_NAME);
            WHERE("ID = #{id}");
        }}.toString();
    }

    /**
     * 统一查询列表SQL
     */
    public String selectList(PbcgzGzdytbDTO dto) {
        StringBuilder sql = new StringBuilder();
        
        // 分页查询需要使用子查询
        if (dto.getPageNum() != null && dto.getPageSize() != null) {
            int offset = (dto.getPageNum() - 1) * dto.getPageSize();
            int limit = dto.getPageSize();
            
            sql.append("SELECT * FROM (");
            sql.append("SELECT ROWNUM AS rn, t.* FROM (");
            sql.append(buildSelectSQL(dto));
            sql.append(" ORDER BY CREATE_TIME DESC");
            sql.append(") t WHERE ROWNUM <= ").append(offset + limit);
            sql.append(") WHERE rn > ").append(offset);
        } else {
            sql.append(buildSelectSQL(dto));
            sql.append(" ORDER BY CREATE_TIME DESC");
        }
        
        return sql.toString();
    }

    /**
     * 查询总数SQL
     */
    public String selectCount(PbcgzGzdytbDTO dto) {
        return new SQL() {{
            SELECT("COUNT(*)");
            FROM(TABLE_NAME);
            
            // 动态条件
            if (!YDPString.isEmpty(dto.getReportWeek())) {
                WHERE("REPORT_WEEK = #{reportWeek}");
            }
            if (!YDPString.isEmpty(dto.getStatus())) {
                WHERE("STATUS = #{status}");
            }
            if (!YDPString.isEmpty(dto.getSubsidiaryCompany())) {
                WHERE("SUBSIDIARY_COMPANY LIKE '%' || #{subsidiaryCompany} || '%'");
            }
            if (!YDPString.isEmpty(dto.getCityBureau())) {
                WHERE("CITY_BUREAU LIKE '%' || #{cityBureau} || '%'");
            }
            if (!YDPString.isEmpty(dto.getGisid())) {
                WHERE("GISID LIKE '%' || #{gisid} || '%'");
            }
            if (!YDPString.isEmpty(dto.getUserId())) {
                WHERE("USER_ID = #{userId}");
            }
            if (!YDPString.isEmpty(dto.getOrgId())) {
                WHERE("ORG_ID = #{orgId}");
            }
        }}.toString();
    }

    /**
     * 构建基础查询SQL
     */
    private String buildSelectSQL(PbcgzGzdytbDTO dto) {
        return new SQL() {{
            SELECT("*");
            FROM(TABLE_NAME);
            
            // 动态条件
            if (!YDPString.isEmpty(dto.getReportWeek())) {
                WHERE("REPORT_WEEK = #{reportWeek}");
            }
            if (!YDPString.isEmpty(dto.getStatus())) {
                WHERE("STATUS = #{status}");
            }
            if (!YDPString.isEmpty(dto.getSubsidiaryCompany())) {
                WHERE("SUBSIDIARY_COMPANY LIKE '%' || #{subsidiaryCompany} || '%'");
            }
            if (!YDPString.isEmpty(dto.getCityBureau())) {
                WHERE("CITY_BUREAU LIKE '%' || #{cityBureau} || '%'");
            }
            if (!YDPString.isEmpty(dto.getGisid())) {
                WHERE("GISID LIKE '%' || #{gisid} || '%'");
            }
            if (!YDPString.isEmpty(dto.getUserId())) {
                WHERE("USER_ID = #{userId}");
            }
            if (!YDPString.isEmpty(dto.getOrgId())) {
                WHERE("ORG_ID = #{orgId}");
            }
        }}.toString();
    }
}